package com.mindmax.user.service.impl;

import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.mindmax.user.domain.YtRole;
import com.mindmax.user.domain.YtUserRole;
import com.mindmax.user.service.IYtRoleService;
import com.mindmax.user.service.IYtUserRoleService;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mindmax.user.mapper.TenantUserMapper;
import com.mindmax.user.domain.TenantUser;
import com.mindmax.user.service.ITenantUserService;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 关联租户列表Service业务层处理
 *
 * @author cabloy
 * @date 2024-04-02
 */
@Service
public class TenantUserServiceImpl implements ITenantUserService
{
    @Autowired
    private TenantUserMapper tenantUserMapper;


    @Autowired
    private IYtUserRoleService ytUserRoleService;


    @Autowired
    private IYtRoleService ytRoleService;

    /**
     * 查询关联租户列表
     *
     * @param id 关联租户列表主键
     * @return 关联租户列表
     */
    @Override
    public TenantUser selectTenantUserById(Long id)
    {
        return tenantUserMapper.selectTenantUserById(id);
    }

    /**
     * 查询关联租户列表列表
     *
     * @param tenantUser 关联租户列表
     * @return 关联租户列表
     */
    @Override
    public List<TenantUser> selectTenantUserList(TenantUser tenantUser)
    {
        return tenantUserMapper.selectTenantUserList(tenantUser);
    }

    /**
     * 新增关联租户列表
     *
     * @param tenantUser 关联租户列表
     * @return 结果
     */
    @Override
    public int insertTenantUser(TenantUser tenantUser)
    {
        tenantUser.setCreateTime(DateUtils.getNowDate());
        return tenantUserMapper.insertTenantUser(tenantUser);
    }

    /**
     * 修改关联租户列表
     *
     * @param tenantUser 关联租户列表
     * @return 结果
     */
    @Override
    public int updateTenantUser(TenantUser tenantUser)
    {
        return tenantUserMapper.updateTenantUser(tenantUser);
    }

    /**
     * 批量删除关联租户列表
     *
     * @param ids 需要删除的关联租户列表主键
     * @return 结果
     */
    @Override
    public int deleteTenantUserByIds(Long[] ids)
    {
        return tenantUserMapper.deleteTenantUserByIds(ids);
    }

    /**
     * 删除关联租户列表信息
     *
     * @param id 关联租户列表主键
     * @return 结果
     */
    @Override
    public int deleteTenantUserById(Long id)
    {
        return tenantUserMapper.deleteTenantUserById(id);
    }

    @Override
    public List<TenantUser> selectTenantUserListByTenantId(Long tenantId) {
        return tenantUserMapper.selectTenantUserByTenantId(tenantId);
    }


    @Override
   public List<Map<String, Object>> selectCountByTenant(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Lists.newArrayList();
        }
        return tenantUserMapper.selectCountByTenant(ids);
    }

    @Override
    public void setUserIsAdmin(Long userId, Long tenantId) {
        List<YtRole> ytRoles = ytRoleService.selectYtRoleByTenantId(tenantId);
        if (CollectionUtils.isEmpty(ytRoles)) {
            return;
        }
        YtRole ytRole = ytRoles.getFirst();

        ytUserRoleService.deleteYtUserRoleByTenantIdAndRoleId(tenantId, ytRole.getId());
        YtUserRole ytUserRole = new YtUserRole();
        ytUserRole.setUserId(userId);
        ytUserRole.setRoleId(ytRole.getId());
        ytUserRole.setTenantId(tenantId);
        ytUserRole.setCreateTime(DateUtils.getNowDate());
        ytUserRole.setCreateUser(getUserId());
        ytUserRole.setUpdateTime(DateUtils.getNowDate());
        ytUserRoleService.insertYtUserRole(ytUserRole);
    }
}
